// ignore_for_file: no_logic_in_create_state

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../../../store/project/project.dart';

class ProjectForm extends StatefulWidget {
  final VoidCallback? onConfirm;

  final Map<String, String>? formData;
  const ProjectForm({super.key,  this.onConfirm, this.formData});

  @override
  State<ProjectForm> createState() {
    _ProjectFormState p = _ProjectFormState(onConfirm: onConfirm);
    // p.setData(formData);
    return p;
  }
}

class _ProjectFormState extends State<ProjectForm> {
  String name = '';
  String path = '';
  String desc = '';
  String title = '新建项目';
  final VoidCallback? onConfirm;

  setData(Map<String, String>? data) {
    if (data == null) {
      return;
    }
    setState(() {
      // name = data['name']!;
      // name = data['path']!;
      // name = data['desc']!;
    });
  }

  _ProjectFormState({required this.onConfirm});

  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(20),
      child: Form(
        key: _formKey,
        child: Column(
          children: renderChildren,
        ),
      ),
    );
  }

  List<Widget> get renderChildren {
    return [
      Expanded(
        child: Column(
            children: insertBetween(
                [
              renderHeader(),
              renderProjectName(),
              renderProjectPath(),
              renderProjectDesc()
            ],
                const SizedBox(
                  height: 20,
                ))),
      ),
      SizedBox(
        height: 40,
        child: renderFooter,
      )
    ];
  }

  // 绘制name字输入框
  TextFormField renderProjectName() {
    return TextFormField(
      decoration: const InputDecoration(
        labelText: '项目名称',
        hintText: '请输入项目名称',
        border: OutlineInputBorder(),
      ),
      onChanged: (value) => setState(() {
        name = value;
      }),
      validator: (value) {
        if (value == null || value.isEmpty) {
          return '请输入项目名称';
        }
        return null;
      },
    );
  }

  // 绘制path输入框
  TextFormField renderProjectPath() {
    return TextFormField(
      decoration: const InputDecoration(
        labelText: '项目路径',
        hintText: '请输入项目路径',
        border: OutlineInputBorder(),
      ),
      onChanged: (value) => setState(() {
        path = value;
      }),
      validator: (value) {
        if (value == null || value.isEmpty) {
          return '请输入项目路径';
        }
        return null;
      },
    );
  }

  renderHeader() {
    return Text(
      title,
      style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
    );
  }

  // 绘制desc输入框
  TextFormField renderProjectDesc() {
    return TextFormField(
      decoration: const InputDecoration(
        labelText: '项目描述',
        hintText: '请输入项目描述',
        border: OutlineInputBorder(),
      ),
      onChanged: (value) => setState(() {
        desc = value;
      }),
      validator: (value) {
        if (value == null || value.isEmpty) {
          return '请输入项目描述';
        }
        return null;
      },
    );
  }

  Widget get renderFooter {
    return Row(
      textDirection: TextDirection.rtl,
      children: [
        ElevatedButton(onPressed: () {}, child: const Text('取消')),
        const SizedBox(
          width: 20,
        ),
        ElevatedButton(
            onPressed: () async {
              bool flag = _formKey.currentState!.validate();
              if (!flag) {
                return;
              }
              await ProjectStore.insertProject(
                  {'name': name, 'path': path, 'desc': desc});
              if (onConfirm != null) {
                onConfirm!();
              }
            },
            child: const Text('确认'))
      ],
    );
  }
}

List<T> insertBetween<T>(List<T> list, T element) {
  List<T> result = [];

  for (int i = 0; i < list.length; i++) {
    result.add(list[i]);
    if (i != list.length - 1) {
      result.add(element);
    }
  }

  return result;
}
